package ai4games_tests.tests;

import game2dai.Domain;
import game2dai.World;
import game2dai.entities.Vehicle;
import game2dai.entityshapes.ps.ArrowPic;
import game2dai.entityshapes.ps.Hints;
import game2dai.maths.Vector2D;
import game2dai.steering.AutoPilot;
import game2dai.steering.SBF;
import processing.core.PApplet;


/**
 * Evade
 * Demonstrates an agent controlled by the mouse. The other agent will attempt to evade it
 * @author Peter Lager
 *
 */
public class GameTest_004 extends PApplet{

	long ct, et, lt;
	float deltaTime;
	
	World w;
	Domain wd;
	
	Vehicle hunted, mouseSeeker;
	ArrowPic view;
	AutoPilot sbEvade, sbFollowMouse;
	
	Vector2D target;
	
	public void setup(){
		size(600,600);
		w = new World(width, height);
		wd = new Domain(0, 0, width, height);
		
		cursor(CROSS);
		target = new Vector2D(width/2, height/2);
		hunted = new Vehicle(new Vector2D(20,200), // position
				6, // collision radius
				new Vector2D(50,0), // velocity
				70, // maximum speed
				new Vector2D(1,0), // heading
				1, // mass
				1,  // turning rate
				200 // max force
				); 
		view = new ArrowPic(this);	
		view.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY);
//		view.fill(color(128,128,255));
		hunted.renderer(view);
		hunted.worldDomain(wd);
		
		mouseSeeker = new Vehicle(new Vector2D(width - 40,height - 50), // position
				6, // collision radius
				new Vector2D(-100,0), // velocity
				75, // maximum speed
				new Vector2D(-1,0), // heading
				1, // mass
				2,  // turning rate
				200 // max force
				); 
		view = new ArrowPic(this);		
		view.showHints(Hints.HINT_HEADING | Hints.HINT_VELOCITY);
//		view.fill(color(255,128,128));
		mouseSeeker.renderer(view);
		
		sbEvade = new AutoPilot();
//		sbEvade.setAgent(SBF.AGENT_TO_EVADE, mouseSeeker);
//		sbEvade.enableBehaviours(SBF.EVADE);
		sbEvade.evadeOn(mouseSeeker);
		target.set(width/2, height/2);
		sbFollowMouse = new AutoPilot();
//		sbFollowMouse.setTarget(SBF.SEEK, target);
//		sbFollowMouse.enableBehaviours(SBF.SEEK);
		sbFollowMouse.seekOn(target);
		mouseSeeker.AP(sbFollowMouse);

		w.add(mouseSeeker);
		w.add(hunted);
		
		ct = lt = millis();
		ellipseMode(CENTER);
	}
	
	public void draw(){
		ct = millis();
		et = ct - lt;
		lt = ct;
		deltaTime = et / 1000.0f;
		background(192,255,192);
		stroke(0);
		fill(0);
		text("Select steering behaviour    [E]vade", 10,20);

		target.set(mouseX, mouseY);
		sbFollowMouse.seekOn(target);

		w.update(deltaTime);
		w.draw();
	}
	
	public void keyPressed(){
		if(key == 'e')
			hunted.AP(sbEvade);

	}
}
